package com.yanqu.road.server.cmd.activity.gourdplant;

import com.yanqu.road.entity.activity.gourdplant.GourdPlantConfig;
import com.yanqu.road.entity.activity.gourdplant.GourdPlantUserData;
import com.yanqu.road.pb.activity.GourdPlantProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.gourdplant.GourdPlantModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.gourdplant.GourdPlantMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_GOURD_PLANT_WATERING, desc = "浇水")
public class GourdPlantWateringCmd extends AbstractCommand {

    int type;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GourdPlantProto.GourdPlantWateringReqMsg reqMsg = GourdPlantProto.GourdPlantWateringReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        GourdPlantConfig config = GourdPlantMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_GOURD_PLANT_ACTIVITY_NO_OPEN;
            return;
        }

        if(type != 1 && type != 2){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        GourdPlantModule module = player.getModule(GourdPlantModule.class);
        GourdPlantUserData userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_GOURD_PLANT_ACTIVITY_NO_UNLOCK;
            return;
        }

        ret = player.getModule(GourdPlantModule.class).watering(type);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            GourdPlantProto.GourdPlantWateringRespMsg.Builder respMsg = GourdPlantProto.GourdPlantWateringRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_GOURD_PLANT_WATERING, respMsg);
        }
    }
}
